package com.fxn.adapters;

import com.fxn.modals.Img;
import com.fxn.pixsample.ResourceTable;
import com.fxn.utils.FileUtils;
import com.fxn.utils.PixelUtil;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.media.image.PixelMap;

import java.util.ArrayList;

/**
 * description : MainProvider 首页的provider
 *
 * @since 2021-04-15
 */
public class MyAdapter extends BaseItemProvider {
    private ArrayList<Img> mList = new ArrayList<>();
    private Context mContext;

    /**
     * 创建MainProvider实例
     *
     * @param context context
     * @param list    list
     */
    public MyAdapter(Context context, ArrayList<Img> list) {
        mContext = context;
        mList = list;
    }

    /**
     * 获取provider的item数量
     *
     * @return provider 的item 数量
     */
    @Override
    public int getCount() {
        return mList.size();
    }

    /**
     * 获取 position处的对象
     *
     * @param position position
     * @return position 处对应的object
     */
    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    /**
     * 获取position处对应的item的id
     *
     * @param position position
     * @return position处对应的item的id
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 获取position位置的component
     *
     * @param position           position
     * @param component          component
     * @param componentContainer componentContainer
     * @return position位置的component
     */
    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Img img = mList.get(position);
        ViewHolder viewHolder = null;
        Component tempComponent = null;
        if (component == null) {
            viewHolder = new ViewHolder();
            tempComponent = LayoutScatter.getInstance(mContext)
                    .parse(ResourceTable.Layout_main_selected_image, null, false);
            ComponentContainer.LayoutConfig layoutConfig = tempComponent.getLayoutConfig();
            layoutConfig.width = ((PixelUtil.screenWidth() - 40) / 2);
            layoutConfig.height = (int) (1.5 * layoutConfig.width);
            layoutConfig.setMargins(10, 10, 10, 10);
            tempComponent.setLayoutConfig(layoutConfig);
            if (tempComponent.findComponentById(ResourceTable.Id_image) instanceof Image) {
                viewHolder.preview = (Image) tempComponent.findComponentById(ResourceTable.Id_image);
            }
            if (tempComponent.findComponentById(ResourceTable.Id_play) instanceof Image) {
                viewHolder.play = (Image) tempComponent.findComponentById(ResourceTable.Id_play);
            }
            tempComponent.setTag(viewHolder);
        } else {
            tempComponent = component;
            if (tempComponent.getTag() instanceof ViewHolder) {
                viewHolder = (ViewHolder) tempComponent.getTag();
            }
        }
        PixelMap pixMap = null;
        ShapeElement se = new ShapeElement();
        if (img.getMediaType().contains("video")) {
            viewHolder.play.setVisibility(Component.VISIBLE);
            se.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor("#ff075e54")));
            pixMap = FileUtils.getFirstPixelMap(img.getUrl());
        } else {
            viewHolder.play.setVisibility(Component.HIDE);
            if ("".equals(img.getContentUrl()) || img.getContentUrl() == null) {
                pixMap = FileUtils.getPixMap(img.getUrl());
            } else {
                pixMap = FileUtils.getPixMapFromUri(img.getContentUrl(), mContext);
            }
            se.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor("#ff075e54")));
        }
        se.setCornerRadius(PixelUtil.vp2px(20));
        viewHolder.preview.setBackground(se);
        viewHolder.preview.setPixelMap(pixMap);
        viewHolder.preview.setCornerRadius(PixelUtil.vp2px(20));
        return tempComponent;
    }

    private static class ViewHolder {
        Image preview;
        Image play;
    }

}
